﻿using System;

namespace Silver3D.Graphics
{
    public class VertexBuffer
    {
        private Vertex[] vertexData;

        public VertexInfo VertexInfo { get; private set; }

        public int Count { get { return vertexData == null ? 0 : vertexData.Length; } }

        public VertexBuffer()
        {
        }

        public VertexBuffer(VertexInfo vertexInfo, int count, double[] data)
        {
            SetData(vertexInfo, count, data);
        }

        public void SetData(VertexInfo vertexInfo, int count, double[] data)
        {
            if (vertexInfo == null)
                throw new ArgumentNullException("vertexInfo");

            if (count < 1)
                throw new ArgumentException("count needs to be at least 1");

            VertexInfo = vertexInfo;
            vertexData = new Vertex[count];

            if (data != null)
            {
                int elementCount = count * vertexInfo.Size;
                if (data.Length < elementCount)
                    throw new ArgumentException("data.Length needs to be at least " + elementCount);

                for (int i = 0, offset = 0; i < count; ++i, offset += vertexInfo.Size)
                {
                    vertexData[i] = new Vertex(vertexInfo, data, offset);
                }
            }
        }

        public Vertex this[int index]
        {
            get { return vertexData[index]; }
            set { vertexData[index] = value; }
        }
    }
}
